#include "ZContainer.h"
#include "ZImage.h"
#include "ZBox.h"
#include "ZText.h"

using namespace Zdib;
#pragma once

TPos GetMousePos();
void LoopGame(int x,int y);
void CreateObj(Zdib::CContainer *parent);
static void DeleteObj(Zdib::CContainer *obj);
char *GetPath(char *filename);
void InitInterface();

void EnterChar(char c);

class CBaseObject
{
public:
	
	CBaseObject(CBaseObject *parent);
	virtual ~CBaseObject()
	{
	}
	void SetEvent(Zdib::CContainer *obj);
	void MoveEvent(Zdib::MouseEvent_Type mouseevent,int wheel=0)
	{
		switch(mouseevent)
		{
		case Zdib::ME_LeftDown:
			LeftDown();
			return;
		case Zdib::ME_LeftUp:
			LeftUp();
			return;
		case Zdib::ME_RightDown:
			RightDown();
			return;
		case Zdib::ME_RightUp:
			RightUp();
			return;
		case Zdib::ME_MiddleWheel:
			WheelMove(wheel);
			return;
		case Zdib::ME_MouseEnter:
			Enter();
			return;
		case Zdib::ME_MouseLeave:
			Leave();
		default:
			return;
		}
	}
	virtual void LeftDown()
	{
		printf("LeftDown\n");
	}
	virtual void LeftUp()
	{
		printf("LeftUp\n");
	}
	virtual void RightDown()
	{
		printf("RightDown\n");
	}
	virtual void RightUp()
	{
		printf("RightUp\n");
	}
	virtual void Enter()
	{
		printf("Enter\n");
	}
	virtual void Leave()
	{
		printf("Leave\n");
	}
	virtual void WheelMove(int wheel)
	{
		printf("WheelMove\n");
	}
	
	CBaseObject *GetEvParent()
	{
		return m_EvParent;
	}
	virtual Zdib::CContainer *GetMapClass()
	{
		return NULL;
	}
	virtual void OnMessage(int msgid)
	{
		printf("OnMessage\n");
	}
protected:
	CBaseObject *m_EvParent;
};

class CObject:public Zdib::CContainer,public CBaseObject
{
public:
	CObject(CContainer *parent);
	virtual ~CObject()
	{}
	virtual LPVOID GetEventClass()
	{
		return (LPVOID)((CBaseObject *)this);
	}
};

class CMyEvent:public Zdib::CEventReceiver
{
public:
	virtual bool OnEvent(const TEvent& ev)
	{
		if(ev.event_type==0)
		{
			CBaseObject *tempbo;
			if(ev.Mouse.Call!=NULL)
			{
				tempbo=(CBaseObject *)ev.Mouse.Call->GetEventClass();
				if(tempbo!=NULL)
					tempbo->MoveEvent(ev.Mouse.mouse_type,ev.Mouse.wheel);
			}
			return true;
		}
		return false;
	}
};

class CClickObject:public CBaseObject
{
public:
	
	CClickObject(CBaseObject *parent,int msgid):CBaseObject(parent)
	{
		m_MsgId=msgid;
	}

	virtual void LeftDown()
	{
		CBaseObject::LeftDown();
	}
	virtual void LeftUp()
	{
		CBaseObject::LeftUp();
		CBaseObject *parent;
		parent=this->GetEvParent();
		if(parent!=NULL)
		{
			parent->OnMessage(this->m_MsgId);
		}
	}
private:
	int m_MsgId;
};

class CHoldObject:public CBaseObject
{
public:
	CHoldObject(CBaseObject *parent):CBaseObject(parent)
	{}
	virtual ~CHoldObject()
	{}

	virtual void LeftDown();
	virtual void LeftUp();
};

class CButton:public Zdib::CImager,public CClickObject
{
public:
	CButton(Zdib::CContainer *parent,char *filename,int msgid=0):Zdib::CImager(parent->GetRoot(),parent,filename),CClickObject((CBaseObject *)(parent->GetEventClass()),msgid)
	{
		SetDirFrame(3,1);
		SetCurDir(0);
		SetCurFrame(0);
		this->SetEvent((Zdib::CContainer *)this);
	}
	virtual ~CButton(){}
	
	virtual LPVOID GetEventClass()
	{
		return (LPVOID)((CBaseObject *)this);
	}

	virtual Zdib::CContainer *GetMapClass()
	{
		return (Zdib::CContainer *)this;
	}

	virtual void LeftDown()
	{
		SetCurDir(2);
		CClickObject::LeftDown();
	}
	virtual void LeftUp()
	{
		SetCurDir(0);
		CClickObject::LeftUp();
	}

	virtual void Enter()
	{
		SetCurDir(1);
		CClickObject::Enter();
	}
	virtual void Leave()
	{
		SetCurDir(0);
		CClickObject::Leave();
	}
};

class CPanel:public Zdib::CImager,public CHoldObject
{
public:
	CPanel(Zdib::CContainer *parent,char *filename):Zdib::CImager(parent->GetRoot(),parent,filename),CHoldObject((CBaseObject *)parent->GetEventClass())
	{
		SetDirFrame(0,0);
		SetCurDir(0);
		SetCurFrame(0);
		this->SetEvent((Zdib::CContainer *)this);
	}
	virtual ~CPanel()
	{}

	virtual LPVOID GetEventClass()
	{
		return (LPVOID)((CBaseObject *)this);
	}

	virtual Zdib::CContainer *GetMapClass()
	{
		return (Zdib::CContainer *)this;
	}
};

class CDialogBase:public Zdib::CContainer,public CHoldObject
{
public:
	
	CDialogBase(Zdib::CContainer *parent,char *fileroot):Zdib::CContainer(parent),CHoldObject((CBaseObject *)parent->GetEventClass())
	{
		SetRoot(parent->GetRoot());
		CreateImg(fileroot);
		this->SetEvent((Zdib::CContainer *)this);
	}

	virtual ~CDialogBase()
	{
	}

	virtual LPVOID GetEventClass()
	{
		return (LPVOID)((CBaseObject *)this);
	}

	virtual Zdib::CContainer *GetMapClass()
	{
		return (Zdib::CContainer *)this;
	}

	void CreateImg(char *fileroot)
	{
		m_img1=new Zdib::CImager(GetRoot(),(Zdib::CContainer *)this,fileroot);
		m_img1->SetDirFrame(1,1);
		m_img1->SetPos(0,0);
		m_img2=new Zdib::CImager(GetRoot(),(Zdib::CContainer *)this,fileroot);
		m_img2->SetDirFrame(1,1);
		m_img2->SetPos(100,0);
		m_btn=new CButton(this,GetPath("\\button.bmp"),1);
	}
	
	virtual void OnMessage(int msgid);
private:
	Zdib::CImager *m_img1,*m_img2;
	CButton *m_btn;
};

class CEdit:public Zdib::CBox
{
public:
	CEdit(Zdib::CContainer *parent,TColor color);
	~CEdit()
	{}
	void CharInput(unsigned char ch);
	void AddChar(unsigned char *pch);
	void KeyBack();
	void KeyEnter();
	void KeyLeft();
	void KeyRight();
	void ShowTxt();
private:
	unsigned char m_LastChar;
	unsigned char m_UnChar[3];
	TZList m_AllTxtObj;
	Zdib::CText *m_LightSign;
	int m_NowPos;
};